import 'package:flutter/material.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';

Future<int> showTipsDialog(BuildContext context,
    {String titleText,
    @required String messageText,
    Color messageColor = color333333,
    String cancelText = "取消",
    Color cancelColor = color666666,
    String okText = "确定",
    Color okColor = colorFACD33,
    bool singleButton = false}) {
  return showDialog<int>(
    context: context,
    barrierDismissible: false,
    builder: (context) => SimpleDialog(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(Adapt.px(10))),
      backgroundColor: Colors.white,
      elevation: 0.0,
      contentPadding: EdgeInsets.all(0),
      children: <Widget>[
        DialogBodyWidget(
          titleText: titleText,
          messageText: messageText,
          messageColor: messageColor,
          cancelText: cancelText,
          cancelColor: cancelColor,
          okText: okText,
          okColor: okColor,
          singleButton: singleButton,
          onCancel: () => Navigator.pop(context, 0),
          onOk: () => Navigator.pop(context, 1),
        ),
      ],
    ),
  );
}

class DialogBodyWidget extends StatelessWidget {
  final String titleText;
  final String messageText;
  final String cancelText;
  final String okText;
  final Color messageColor;
  final Color okColor;
  final Color cancelColor;
  final bool singleButton;
  final VoidCallback onCancel;
  final VoidCallback onOk;

  const DialogBodyWidget(
      {Key key,
      this.titleText,
      this.messageText,
      this.cancelText,
      this.okText,
      this.messageColor,
      this.okColor,
      this.cancelColor,
      this.singleButton,
      this.onCancel,
      this.onOk})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: Adapt.getScreenWidth,
      padding: EdgeInsets.only(top: Adapt.px(24)),
      child: Column(
        children: <Widget>[
          titleText != null
              ? Text(
                  titleText,
                  style: TextStyle(
                      color: color333333,
                      fontSize: Adapt.px(18),
                      fontWeight: FontWeight.w600),
                )
              : Container(),
          SizedBox(height: Adapt.px(9)),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: Adapt.px(16)),
            child: Text(
              messageText,
              textAlign: TextAlign.center,
              style: TextStyle(
                color: color333333,
                fontSize: Adapt.px(16),
              ),
            ),
          ),
          SizedBox(height: Adapt.px(24)),
          Divider(height: Adapt.px(2), color: colorE5E5E5),
          SizedBox(
            height: Adapt.px(50),
            child: Row(
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                singleButton
                    ? Container()
                    : Expanded(
                        child: ButtonTheme(
                          height: double.infinity,
                          child: FlatButton(
                            onPressed: onCancel,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.only(
                                topLeft: Radius.zero,
                                topRight: Radius.zero,
                                bottomRight: Radius.zero,
                                bottomLeft: Radius.circular(Adapt.px(10)),
                              ),
                            ),
                            materialTapTargetSize:
                                MaterialTapTargetSize.shrinkWrap,
                            child: Text(
                              cancelText,
                              style: TextStyle(
                                  color: cancelColor,
                                  fontSize: Adapt.px(18),
                                  fontWeight: FontWeight.normal),
                            ),
                          ),
                        ),
                      ),
                singleButton
                    ? Container()
                    : VerticalDivider(width: Adapt.px(2), color: colorE5E5E5),
                Expanded(
                  child: ButtonTheme(
                    height: double.infinity,
                    child: FlatButton(
                      onPressed: onOk,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.zero,
                          topRight: Radius.zero,
                          bottomRight: Radius.circular(Adapt.px(10)),
                          bottomLeft: singleButton
                              ? Radius.circular(Adapt.px(10))
                              : Radius.zero,
                        ),
                      ),
                      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                      child: Text(
                        okText,
                        style: TextStyle(
                            color: okColor,
                            fontSize: Adapt.px(18),
                            fontWeight: FontWeight.normal),
                      ),
                    ),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
